
import pygame
import pygameWindowInfo
import os

class PygameGraphSys():
	def __init__(self, **kwargs):
		self.isopend = False
		self.set_optinos(**kwargs)
		self.states = {}
		
	
	def set_optinos(self, **kwargs):
		self.resizeable = pygame.RESIZABLE
		self.hasborder = not pygame.NOFRAME
		self.icon = None
		self.caption = None
		screenx, screeny, screenwidth, screenheight = 128, 128, 1024, 768
		self.size = self.width, self.height = screenwidth, screenheight
		os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (screenx, screeny)
		
		
	def open(self):
		if self.isopend:
			return
			
		pygame.display.init()
		flag = 0
		if self.resizeable:
			flag | pygame.RESIZABLE
		if not self.hasborder:
			flag | pygame.NOFRAME
			
		self.screen = pygame.display.set_mode(self.size)
		self.winInfo = pygameWindowInfo.PygameWindowInfo()
		self.isopend = True
		
	def reopen(self, **kwargs):
		self.close()
		self.set_optinos(**kwargs)
		
	def close(self):
		if not self.isopend or not pygame.display.get_init():
			return
		pygame.display.quit()
		self.isopend = False
		
		
	def flip(self):
		if not self.isopend or not pygame.display.get_init():
			return True
			
		self.winInfo.update()
		
		for event in pygame.event.get((pygame.QUIT, pygame.VIDEORESIZE)):
			if event.type == pygame.QUIT:
				self.close()
			if event.type == pygame.VIDEORESIZE and self.resizeable:
				WIDTH, HEIGHT = event.size
				self.screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.RESIZABLE)
			if event.type == pygame.VIDEOEXPOSE:
				pass
			if event.type ==  pygame.ACTIVEEVENT:
				pass
				
		winPos = self.winInfo.getWindowPosition()
		screenPos = self.winInfo.getScreenPosition()
				
		pygame.display.flip()
		#pygame.display.update(rectangle=None)
		return False
		
	def _surface(self):
		if not self.isopend or not pygame.display.get_init():
			return None
		return self.screen
		
class PygameBarn():
	"""
	>>> barn = PygameBarn()
	>>> str(barn.fetch("test128x128_32_1"))
	"<Surface(128x128x32 SW)>"
	>>> "<Sound object at" in str(barn.fetch("close_door_2"))
	True
	"""
	def __init__(self):
		self.library = {}
		self.path = b""
		
	def fetch(self, key):
		if key in self.library:
			return self.library[key][1]
		else:
		
			try:
				'''
				rawsurface = pygame.image.load(os.path.join(self.path, key + ".bmp"))
				image = PygameImage()
				image._raw_surface = rawsurface
				self.library[key] = image
				'''
				path = os.path.join(self.path, key)#+ b".jpg")
				surface = pygame.image.load(path)
				lastmtime = os.stat(path).st_mtime
				self.library[key] = (path, surface, lastmtime)
				return self.library[key][1]
			except pygame.error:
				pass
				
			try:
				path = os.path.join(self.path, key + b".wav")
				rawsound = pygame.mixer.Sound(path)
				sound = PygameSound()
				sound._raw_surface = rawsound
				lastmtime = os.stat(path)
				self.library[key] = (path, sound, lastmtime)
				return self.library[key]
			except pygame.error:
				pass
				
			return None
			
	def autoreload(self):
		reloadkeys = []
		for key in self.library.keys():
			path = self.library[key][0]
			if os.stat(path).st_mtime != self.library[key][2]:
				reloadkeys.append(key)
			
		# reload
		for key in reloadkeys:
			del self.library[key]
			self.fetch(key)

barn = PygameBarn()